package com.umeox.watch.moto.settings.utils;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.umeox.moto.common.log.LogUtils;

import java.util.Locale;

public class CommonUtils {


    @SuppressLint({"MissingPermission", "HardwareIds"})
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    public static String getMcc(Context context) {
        TelephonyManager mTelNet = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = mTelNet.getNetworkOperator();
        if (!TextUtils.isEmpty(operator) && operator.length() > 3) {
            return operator.substring(0, 3);
        }
        return "";
    }

    /**
     * 修改语言设置
     *
     * @param code
     */
    public static void setLocalLanguage(Object code) {
        try {
            Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative");

            Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative);

            Object config = am.getClass().getMethod("getConfiguration").invoke(am);

            config.getClass().getDeclaredField("locale").set(config, code);
            config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
            am.getClass().getMethod("updateConfiguration", android.content.res.Configuration.class).invoke(am, config);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getCurrentNetType(Context context) {
        String netType = "";
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        boolean isRoaming = telephonyManager.isNetworkRoaming();

        if (isRoaming) {
            netType = "roaming";
        } else {
            if (info == null) {
                netType = "null";
            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                netType = "wifi";
            } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                int subType = info.getSubtype();
                if (subType == TelephonyManager.NETWORK_TYPE_CDMA
                        || subType == TelephonyManager.NETWORK_TYPE_GPRS) {
                    netType = "2g";
                } else if (subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                    netType = "e";
                } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS
                        || subType == TelephonyManager.NETWORK_TYPE_HSDPA       //NETWORK_TYPE_HSPAP
                        || subType == TelephonyManager.NETWORK_TYPE_EVDO_A
                        || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
                        || subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
                    netType = "3g";
                } else if (subType == TelephonyManager.NETWORK_TYPE_HSPAP) {
                    netType = "h";
                } else if (subType == TelephonyManager.NETWORK_TYPE_HSPA) {
                    netType = "h1";

                } else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
                    netType = "4g";
                }
            }
        }
        return netType;
    }

    /**
     * 获取系统当前语言环境
     *
     * @return
     */
    public static String getCurrentSystemLanguage() {
        Locale l = Locale.getDefault();
        String language = l.getLanguage();
        String country = l.getCountry().toLowerCase();
        String currentLanguage = "English";
        switch (language) {
            case "zh":
                if ("cn".equals(country)) {
                    currentLanguage = "中文简体";
                } else if ("tw".equals(country)) {
                    currentLanguage = "中文繁體";
                }
                break;
            case "af":
                return "Dutch";
            case "cs":
            return "Čeština";
            case "da":
                currentLanguage = "Dansk";
                break;
            case "de":
                currentLanguage = "Deutsch";
                break;
            case "en":
                currentLanguage = "English";
                break;
            case "es":
                currentLanguage = "Español";
                break;
            case "fi":
                currentLanguage = "Suomi";
                break;

            case "fr":
                currentLanguage = "Français";
                break;
            case "hu":
                currentLanguage = "Hungarian";
                break;
            case "et":
                currentLanguage = "Eesti";
                break;
            case "ja":
                if ("jp".equals(country)) {
                    return  "にほんご";
                }
                return  "日本語";

            case "th":
                currentLanguage = "ภาษาไทย";
                break;
            case "ru":
                currentLanguage = "Русский";
                break;

            case "sv":
                currentLanguage = "Svenska";
                break;
            case "nl":
                currentLanguage = "Nederlands";
                break;
            case "no":
                currentLanguage = "Norsk";
                break;
            case "tr": //土耳其语
                currentLanguage = "Turkish";
                break;
            case "uk":
                currentLanguage = "Українська";
                break;
            case "pl":
                currentLanguage = "Polish";
                break;
            case "lv":
                currentLanguage = "Latviešu";
                break;
            case "lt":
                currentLanguage = "Lietuvių";
                break;
        }
        return currentLanguage;
    }


    public static boolean getWifiState(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            return wifiManager.isWifiEnabled();
        }
        return false;
    }

    public static void setWifiState(Context context, boolean bool) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (wifiManager != null) {
            wifiManager.setWifiEnabled(bool);
        }
    }


    public static String getCurrentApnName(Context context) {
        ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        //获取网络接入点，中国移动:cmwap和cmnet; 中国电信ctwap，ctnet
        if (ni != null) {
            LogUtils.i("info:" + ni.toString());
            return ni.getExtraInfo();
        }
        return "unknow";
    }

    public static boolean hasIccCard(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        if (tm != null) {
            return tm.hasIccCard();
        }
        return false;
    }
}
